package JUC.CAS;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicReference;


/**
 * @author CKC
 * @DateTime 2021/3/18 21:39
 * <p>
 * <p>
 * AtomicInteger
 * *   JUC.CAS--->Unsafe-->CAS底层思想-->ABA---->原子引用更新--->如何规避ABA问题
 * *    ABA:  狸猫换太子
 * <p>
 * AtomicReference<V>  原子引用包装类  V:泛型，需要原子包装的类
 */
@Getter
@NoArgsConstructor
class User {
    public String userName;
    public int age;
    
    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                '}';
    }
}


public class AtomicReferenceDemo {
    
    public static void main(String[] args) {
        User z3 = new User("zhangsan", 12);
        User li4 = new User("lisi", 78);
        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(z3);
        System.out.println(atomicReference.compareAndSet(z3, li4)+"\t "+atomicReference.get().toString());
        System.out.println(atomicReference.compareAndSet(z3, li4)+"\t "+atomicReference.get().toString());
    
    }
}
